/*
在线人数
每小时为单位的粗鲁统计，用来给游戏后台提供监控数据。
*/
"use strict"
const mvc = require("cosjs.mvc")
const dbase = mvc.model.require("dbase");

const TTL = 20 * 3600;




exports.set = async function (sid, uid) {
	let date = new Date();
	let _h = date.getHours();
	let set = new dbase.set(sid, getDatePre(_h));
	let _ttl = await set.ttl();
	if (_ttl == -1) {
		await set.expire(TTL);
	}
	set.set(uid);
}

//当前在线人数
exports.now = function (sid) {
	let date = new Date();
	let _h = date.getHours();
	let set = new dbase.set(sid, getDatePre(_h));
	return set.size();
}

exports.list = async function (sid) {
	let date = new Date();
	let _h = date.getHours();
	let _list = {};
	for (let i = 0; i <= _h; ++i) {
		let set = new dbase.set(sid, getDatePre(i));
		let _ret = await set.size();
		_list[i] = _ret;
	}
	return _list;
}

function getDatePre(_h) {
	if (_h < 10) {
		_h = `0${_h}`;
	}
	return "online" + _h;
}

// //历史在线人数，m月d日h小时
// exports.history=function(sid,m,d,h){
// 	let date=new Date();
// 	let skye=`${m}${d}-${h}`;
//     let set = new dbase.set(sid,"online"+skye);
// 	return set.size();
// }

